package com.englisheveryday.activity;

import java.util.HashMap;
import java.util.Set;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.englisheveryday.R;
import com.englisheveryday.media.SimpleMediaPlayerService;
import com.englisheveryday.utils.ActivityManager;
import com.englisheveryday.utils.L;

/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-10-12 上午9:15:40
 * @version 1.1
 */

public class MainActivity extends ActivityGroup {

	private HashMap<String,View> tabMap = new HashMap<String, View>();
	private static final String TABSPEC_ENGLISHEVERYDAY = "everyday";
	private static final String TABSPEC_BEAUTIFUL = "beautifularticle";
	private static final String TABSPEC_APPSETTING = "appsetting";
	private RelativeLayout exitLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity_tab);
		ActivityManager.add(this);
		initTabMap();
		
		Intent intent_everyday = new Intent();
		intent_everyday.setClass(this,ArticleActivity.class);
		Intent intent_beautifulArticle = new Intent();
		intent_beautifulArticle.setClass(this, PlayListActivity.class);
		Intent intent_settting = new Intent();
		intent_settting.setClass(this, SettingActivity.class);
		
		TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
		tabHost.setup(getLocalActivityManager());
		tabHost.addTab(tabHost.newTabSpec(TABSPEC_ENGLISHEVERYDAY).setIndicator(tabMap.get(TABSPEC_ENGLISHEVERYDAY)).setContent(intent_everyday));
		tabHost.addTab(tabHost.newTabSpec(TABSPEC_BEAUTIFUL).setIndicator(tabMap.get(TABSPEC_BEAUTIFUL)).setContent(intent_beautifulArticle));
		tabHost.addTab(tabHost.newTabSpec(TABSPEC_APPSETTING).setIndicator(tabMap.get(TABSPEC_APPSETTING)).setContent(intent_settting));
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				//将全部标签重置
				Set<String> keySet = tabMap.keySet();
				for(String key:keySet){
					View view = tabMap.get(key);
					view.findViewById(R.id.tabbar_selected_line).setBackgroundDrawable(null);
					((TextView)view.findViewById(R.id.tabbar_text)).setTextColor(getResources().getColor(R.color.white));
					view.findViewById(R.id.tabbar_bottomline).setVisibility(View.GONE);
				}
				//将选中标签设置为选中
				((TextView)tabMap.get(tabId).findViewById(R.id.tabbar_text)).setTextColor(getResources().getColor(R.color.yellow));
				tabMap.get(tabId).findViewById(R.id.tabbar_bottomline).setVisibility(View.VISIBLE);
			}
		});
		tabHost.setCurrentTab(0);
	}
	//当某个Tab被选中的时候
	private void setTabSelected(View view){
		
	}
	//init TabMap
	private void initTabMap(){
		tabMap.put(TABSPEC_ENGLISHEVERYDAY, newEveryDay());
		tabMap.put(TABSPEC_BEAUTIFUL, newBeautifulArticle());
		tabMap.put(TABSPEC_APPSETTING,newAppSetting());
	}
	
	
	//设置TabBar的样式【每日口语】
	private View newEveryDay(){
		View everydayTab = loadTabBarView();
		TextView tabbar_text = (TextView)everydayTab.findViewById(R.id.tabbar_text);
		tabbar_text.setText(R.string.main_tab_article);
		tabbar_text.setTextColor(getResources().getColor(R.color.yellow));
		everydayTab.findViewById(R.id.tabbar_bottomline).setVisibility(View.VISIBLE);
		return everydayTab;
	}
	
	//设置TabBar的样式【美文欣赏】
	private View newBeautifulArticle(){
		View beautifulTab = loadTabBarView();
		TextView tabbar_text = (TextView)beautifulTab.findViewById(R.id.tabbar_text);
		tabbar_text.setText(R.string.main_tab_playlist);
		return beautifulTab;
	}
	
	//设置TabBar的样式【系统设置】
	private View newAppSetting(){
		View appSettingTab = loadTabBarView();
		TextView tabbar_text = (TextView)appSettingTab.findViewById(R.id.tabbar_text);
		tabbar_text.setText(R.string.main_tabbar_setting);
		return appSettingTab;
	}
	
	//载入TabBarView
	private View loadTabBarView(){
		return (View)LayoutInflater.from(this).inflate(R.layout.main_activity_tab_item, null);
	}
	
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if(exitLayout!=null){
			if(exitLayout.getVisibility() == View.VISIBLE)
				exitLayout.setVisibility(View.GONE);
			else
				exitLayout.setVisibility(View.VISIBLE);
		}else {
			 exitLayout  = (RelativeLayout)findViewById(R.id.exitButton);
			 exitLayout.setVisibility(View.VISIBLE);
			 exitLayout.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getApplicationContext(),SimpleMediaPlayerService.class);
					getApplicationContext().stopService(intent);
					ActivityManager.destroy();
				}
			});
		}
		return super.onPrepareOptionsMenu(menu);
	}


	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(event.getKeyCode() == KeyEvent.KEYCODE_MENU){
			return false;
		}else if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
			if(exitLayout!=null&&exitLayout.getVisibility() == View.VISIBLE){
				exitLayout.setVisibility(View.GONE);
				return true;
			}
				return super.dispatchKeyEvent(event);
		}
		return false;
	}
	
	
	
}

